/*
  This file is part of 'Trivial chess'.

  'Trivial chess' is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  'Trivial chess' is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with 'Trivial chess'.  If not, see <http://www.gnu.org/licenses/>.
*/

/* SVN FILE: $Id$ */
/**
 * @author         $Author$
 * @version        $Rev$
 * @lastrevision   $Date$
 * @filesource     $URL$
 */

#ifndef PAWNMOVEVEC_H_
#define PAWNMOVEVEC_H_

#include <boost/cstdint.hpp>
#include <boost/array.hpp>

#include "AffectAbstract.h"

template<class Br, class Sq, class Mn, class Vc, typename Int = int8_t>
class PawnMoveVec
	: public AffectAbstract<PawnMoveVec<Br,Sq,Mn,Vc,Int>,Br,Sq,Mn> {

public:
	typedef Sq Square;
	typedef Vc SquareVector;
	typedef typename SquareVector::FileScalar FileScalar;
	typedef typename SquareVector::RankScalar RankScalar;
private:

#	define _(_F,_R) SquareVector(FileScalar(_F),RankScalar(_R))
	static const SquareVector PAWN_VECTOR(const Color color) {
		return color.isWhite() ? _(0,1) : _(0,-1);
	}
	static const boost::array<const SquareVector, 2> PAWN_ATTACK_VECTORS(const Color color) {
		if ( color.isWhite() ) {
			const boost::array<const SquareVector, 2> ret = {{_(1,1),_(-1,1)}};
			return ret;
		}
		else {
			const boost::array<const SquareVector, 2> ret = {{_(1,-1),_(-1,-1)}};
			return ret;
		}
	}
#	undef _

public:
	PawnMoveVec<Br,Sq,Mn,Vc,Int>() : i(-1) {}

	PawnMoveVec<Br,Sq,Mn,Vc,Int>(const Board& board, const Square sq, const Man mn)
		: board(&board), square(sq), man(mn), i(3) {

		this->me().increment();
	}

	void increment() {
		switch ( this->i ) {
		case 3: {
			const SquareVector v = this->PAWN_VECTOR(this->man.color());
			this->attack = this->square + v;
			if ( this->board->isEmpty(this->attack) )
				this->i = 2;
			else {
				this->i = 1;
				this->me().increment();
			}
			return;
		}
		case 2: {
			const SquareVector v = this->PAWN_VECTOR(this->man.color());
			this->i = 1;
			this->attack = this->attack + v;
			if ( !(((this->man.isWhite() && this->square.rank() == Rank::R2())
							|| (this->man.isBlack() && this->square.rank() == Rank::R7()))
						&& this->board->isEmpty(this->attack)) ) {

				this->me().increment();
			}
			return;
		}
		case 1:
		case 0: {
			const SquareVector v = this->PAWN_ATTACK_VECTORS(this->man.color())[this->i];
			this->attack = this->square + v;
			--this->i;
			if ( this->attack.isNil() || this->board->isEmpty(this->attack)
					|| (*this->board)[this->attack].color() == this->man.color() ) {
				this->me().increment();
			}
			return;
		}
		default:
			return;
		}
	}

	bool equal(PawnMoveVec const& other) const {
		return this->i == other.i;
	}

	const Square dereference() const {
		return this->attack;
	}

protected:
	const Board* board;
	Square square;
	Man man;
	Square attack;
	Int i;

};

#endif /* PAWNMOVEVEC_H_ */
